package com.dji.sample.wayline.controller;

import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.wayline.model.entity.DevicePlayTaskEntity;
import com.dji.sample.wayline.service.DevicePlayTaskDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;

/**
 * @author yjt
 * @date 2022-10-26
 * @description:
 */
@RestController
@RequestMapping("${url.wayline.prefix}${url.wayline.version}/task")
public class DevicePlayTaskController {

    @Autowired
    DevicePlayTaskDetailService taskDetailService;


    @PostMapping("/start")
    public ResponseResult<?> startTask(@RequestParam(name = "lineId") Long lineId, @RequestParam(name = "deviceNo") String deviceNo, HttpServletRequest servletRequest) {
        CustomClaim customClaim = (CustomClaim) servletRequest.getAttribute(TOKEN_CLAIM);
        try {
            Long taskId = taskDetailService.startTask(lineId, deviceNo, customClaim.getOrgId(), customClaim.getOrgCode(), customClaim.getUsername());
            return ResponseResult.success(taskId);
        } catch (Exception e) {
            return ResponseResult.error(e.getMessage());
        }
    }

    @PostMapping("/end")
    public ResponseResult<?> endTask(@RequestParam(name = "taskId") Long taskId, @RequestParam(name = "deviceNo") String deviceNo) {
        try {
            taskDetailService.endTask(taskId, deviceNo);
            return ResponseResult.success("操作成功");
        } catch (Exception e) {
            return ResponseResult.error(e.getMessage());
        }
    }

    @PostMapping("/unFinishTask")
    public ResponseResult<List<DevicePlayTaskEntity>> unFinishTask(@RequestParam(name = "lineId", required = false) Long lineId, @RequestParam(name = "deviceNo") String deviceNo) {
        return ResponseResult.success(taskDetailService.unFinishTask(lineId, deviceNo));
    }

    @PostMapping("/getKmlUrl")
    public ResponseResult<?> getKmlUrl(@RequestParam(name = "taskId") Long taskId) throws Exception {
        return ResponseResult.success(taskDetailService.getOssPath(taskId));
    }
}
